//
//  FKFeedbackVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/22.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKFeedbackVC.h"
#import "FKPlaceholderTextView.h"

@interface FKFeedbackVC ()<UITextFieldDelegate>

@property (nonatomic, weak) FKPlaceholderTextView *textView;
@property (nonatomic, weak) UILabel *bottomLabel;
@property (nonatomic, weak) UIButton *bottomBtn;
@property (nonatomic, weak) UITextField *mobileField;

@end

@implementation FKFeedbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textValueChanged:) name:UITextViewTextDidChangeNotification object:nil];
    
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, fk_adjustW(10), fkScreenW, fk_adjustW(208))];
    [self.view addSubview:container];
    container.backgroundColor = [UIColor whiteColor];
    
    FKPlaceholderTextView *textView = [[FKPlaceholderTextView alloc] init];
    [container addSubview:textView];
    self.textView = textView;
    textView.font = fk_adjustFont(14);
    textView.placeholder = @"请在此留下您宝贵的意见。";
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textValueDidChanged:) name:uitextview object:textView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textValueDidChanged:) name:UITextViewTextDidChangeNotification object:textView];
    
    UILabel *bottomLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999 text:@"0/150"];
    [container addSubview:bottomLabel];
    self.bottomLabel = bottomLabel;
    
    UIView *mobileView = [[UIView alloc] init];
    [self.view addSubview:mobileView];
    mobileView.backgroundColor = [UIColor whiteColor];
    
    UILabel *mobileLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"手机号码"];
    [mobileView addSubview:mobileLabel];
    
    UITextField *mobileField = [[UITextField alloc] init];
    [mobileView addSubview:mobileField];
    self.mobileField = mobileField;
    mobileField.delegate = self;
    mobileField.placeholder = @"请填写手机号码";
    mobileField.font = fk_adjustFont(14);
    
    UIButton *bottomBtn = [UIButton fk_btnWithTarget:self action:@selector(bottomBtnClicked:) font:fk_adjustFont(16) normalTitle:@"提交" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
    [self.view addSubview:bottomBtn];
    self.bottomBtn = bottomBtn;
    [bottomBtn fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    bottomBtn.enabled = false;
    
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(fk_adjustW(-12));
        make.right.offset(fk_adjustW(-12));
    }];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(8);
        make.right.offset(-8);
        make.top.offset(0);
        make.bottom.equalTo(bottomLabel.mas_top).offset(-10);
    }];
    [mobileView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(container.mas_bottom).offset(10);
        make.height.offset(fk_adjustW(44));
    }];
    __block CGFloat labelW = [mobileLabel.text sizeWithAttributes:@{NSFontAttributeName : mobileLabel.font}].width + 4;
    [mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.centerY.offset(0);
        make.width.offset(labelW);
    }];
    [mobileField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(mobileLabel.mas_right).offset(8);
        make.top.bottom.offset(0);
        make.right.offset(-12);
    }];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.height.offset(fk_adjustW(44));
        make.top.equalTo(container.mas_bottom).offset(fk_adjustW(80));
    }];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textValueDidChanged:(NSNotification *)noti
{
    id obj = noti.object;
    if ([obj isEqual:self.textView]) {
        NSString *finalText = self.textView.text;
        if (finalText.length > 150) {
            self.textView.text = [finalText substringToIndex:150];
        }
        self.bottomLabel.text = fkFORMAT(@"%zd/150", self.textView.text.length);
    }
}

- (void)bottomBtnClicked:(UIButton *)btn
{
    [self.view endEditing:true];
    if (self.mobileField.text.length != 11) {
        [SVProgressHUD showInfoWithStatus:@"手机号格式错误"];
        return;
    }
    
    btn.enabled = false;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"content"] = self.textView.text;
    param[@"mobile"] = self.mobileField.text;
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=option" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:result.msg];
            fkPopVC;
        }
        btn.enabled = true;
    } failure:^(NSError *error) {
        btn.enabled = true;
    }];
}

- (void)textValueChanged:(NSNotification *)noti
{
    self.bottomBtn.enabled = self.textView.text.length > 0;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *finalText = [textField.text stringByAppendingString:string];
    if (finalText.length <= 11 && [finalText fk_isPureInt]) {
        return true;
    }
    return false;
}



@end
